﻿(function () {
    $(function () {

        var _$categoriesTable = $('#CategoriesTable');
        var _categoryService = abp.services.app.category;

        var _permissions = {
            create: abp.auth.hasPermission('Pages.Category.Create'),
            edit: abp.auth.hasPermission('Pages.Category.Edit'),
            'delete': abp.auth.hasPermission('Pages.Category.Delete')
        };

        var _createModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Mpa/Category/CreateModal',//加载视图
            scriptUrl: abp.appPath + 'Areas/Mpa/Views/Category/_CreateModal.js',//加载对应js
            modalClass: 'CreateCategoryModal'
        });

        var _editModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Mpa/Category/EditModal',
            scriptUrl: abp.appPath + 'Areas/Mpa/Views/Category/_EditModal.js',
            modalClass: 'EditCategoryModal'
        });

        _$categoriesTable.jtable({
            title: app.localize('CategoryManager'),//标题
            paging: true,//启用分页
            sorting: true,//启用排序
            multiSorting: true,//启用多列排序
            actions: {
                listAction: {
                    method: _categoryService.getCategories//获取列表的方法
                }
            },
            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),//操作列
                    width: '15%',
                    sorting: false,
                    display: function (data) {
                        var $span = $('<span></span>');
                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function() {
                                    _editModal.open({ id: data.record.id });
                                });
                        }
                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                .appendTo($span)
                                .click(function() {
                                    deleteCategory(data.record);
                                });
                        }
                        return $span;
                    }
                },
                name: {
                    title: app.localize('Category_Name'),
                    width: '20%'
                }
            }
        });
        //获取列表
        function getCategories(reload) {
            if (reload) {
                _$categoriesTable.jtable('reload');
            } else {
                _$categoriesTable.jtable('load', {
                    filter: $('#CategoriesTableFilter').val()
                });
            }
        }
        //删除分类
        function deleteCategory(category) {
            abp.message.confirm(
                app.localize('CategoryDeleteWarningMessage', category.name),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _categoryService.deleteCategory({
                            id: category.id
                        }).done(function () {
                            getCategories();
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }

        //页面加载完执行
        getCategories();

        //新建分类点击事件
        $('#CreateNewCategoryButton').click(function () {
            _createModal.open();
        });
        //搜索点击事件
        $('#GetCategoriesButton').click(function (e) {
            //取消事件的默认动作
            e.preventDefault();
            getCategories();
        });

        //事件注册
        abp.event.on('app.createCategoryModalSaved', function () {
            getCategories(true);
        });

        abp.event.on('app.editCategoryModalSaved', function () {
            getCategories(true);
        });
    });
})();